# Drop NA---------------------------------------------------------------
ssm_filtered <- ssm_filtered |> 
  dplyr::select(id, SubjHealth, ParentHighEdu, ChildCollege_Max, Sex, BirthCohort, Age, Education, Place_15, Jsei_Fj, EmpStatus_Fj, Unemp, MarStatus, PastHealthIssue)

ssm_filtered <- ssm_filtered |> 
  tidyr::drop_na()

# Descriptive Statistics------------------------------------------------

DescriptiveStatistics <- ssm_filtered |> 
  dplyr::mutate(`Subjective Health` = SubjHealth, 
                `Parental College` = ParentHighEdu, 
                `Child College` = forcats::fct_recode(factor(ChildCollege_Max), "Less than university" = "0", "University or more" = "1"), 
                `Birth Cohort` = BirthCohort,
                `Place at 15` = Place_15,
                `JSEI at the first job` = Jsei_Fj, 
                `Employment status at the first job` = EmpStatus_Fj, 
                `Unemployment` = Unemp, 
                `Marital status` = MarStatus, 
                `Job left by health issue` = PastHealthIssue, 
                `Age Category` = dplyr::case_match(Age, 50:54 ~ "50-54", 55:59 ~ "55-59", 60:64 ~ "60-64", 65:69 ~ "65-69", 70:74 ~ "70-74", 75:80 ~ "75-80")) |> 
  (\(.)modelsummary::datasummary(`Subjective Health` + `Parental College` + `Child College` + Sex + `Birth Cohort` + `Age Category` + Education + `Place at 15` + `JSEI at the first job` + `Employment status at the first job` + `Unemployment` + `Marital status` + `Job left by health issue` ~ 1 + Percent() + Mean + SD, data = ., output = 'markdown'))()

DescriptiveStatistics

# Bar Graph by Parent's Education on Health----------------------

plot_edupa_health <- ssm_filtered |> 
  dplyr::select(ParentHighEdu, SubjHealth, Sex) |> 
  dplyr::group_by(ParentHighEdu) |> 
  dplyr::summarise(Health = round(mean(SubjHealth), 2), 
                   se = sd(SubjHealth) / sqrt(length(SubjHealth)), 
                   conf_low = Health - 1.96 * se, 
                   conf_high = Health + 1.96 * se) |> 
  dplyr::mutate(Sex = "Overall")

plot_edupa_health <- ssm_filtered |> 
  dplyr::select(ParentHighEdu, SubjHealth, Sex) |> 
  dplyr::group_by(ParentHighEdu, Sex) |> 
  dplyr::summarise(Health = round(mean(SubjHealth), 2), 
                   se = sd(SubjHealth) / sqrt(length(SubjHealth)), 
                   conf_low = Health - 1.96 * se, 
                   conf_high = Health + 1.96 * se) |> 
  dplyr::bind_rows(plot_edupa_health)

plot_edupa_health <- plot_edupa_health |> 
  dplyr::mutate(ChildCollege_Max = 2)

# Bar Graph by Parent's and Child's College on Health----------------------

plot_edupa_educh_health <- ssm_filtered |> 
  dplyr::select(ParentHighEdu, ChildCollege_Max, SubjHealth) |> 
  dplyr::summarise(Health = round(mean(SubjHealth), 2), 
                   se = sd(SubjHealth) / sqrt(length(SubjHealth)), 
                   conf_low = Health - 1.96 * se, 
                   conf_high = Health + 1.96 * se, 
                   .by = c(ParentHighEdu, ChildCollege_Max)) |> 
  dplyr::mutate(Sex = "Overall")

plot_edupa_educh_health <- ssm_filtered |> 
  dplyr::select(ParentHighEdu, ChildCollege_Max, SubjHealth, Sex) |> 
  dplyr::summarise(Health = round(mean(SubjHealth), 2), 
                   se = sd(SubjHealth) / sqrt(length(SubjHealth)), 
                   conf_low = Health - 1.96 * se, 
                   conf_high = Health + 1.96 * se, 
                   .by = c(ParentHighEdu, ChildCollege_Max, Sex)) |> 
  dplyr::bind_rows(plot_edupa_educh_health)

plot_ohg <- plot_edupa_health |> dplyr::bind_rows(plot_edupa_educh_health)

plot_ohg |> 
  dplyr::mutate(Sex = forcats::fct_relevel(Sex, "Overall", "Male", "Female"), 
                ChildCollege_Max = forcats::fct_recode(factor(ChildCollege_Max), "Less than university" = "0", "University or more" = "1", "Overall" = "2"), 
                ChildCollege_Max = forcats::fct_relevel(ChildCollege_Max, "Overall", "Less than university", "University or more")) |> 
  dplyr::rename(`Children's Education` = ChildCollege_Max) |> 
  ggplot2::ggplot(ggplot2::aes(x = ParentHighEdu, y = Health, color = `Children's Education`, shape = `Children's Education`)) +
  ggplot2::geom_point(position = position_dodge(width = 0.3), size = 3.5) + 
  ggplot2::geom_errorbar(aes(ymin = conf_low, ymax = conf_high), width = .2, position = position_dodge(width = 0.3), size = 1.2) + 
  ggplot2::facet_wrap(~ Sex) +
  ggplot2::labs(x = "Parent's Education", y = "Mean of Subjective Health") +
  ggplot2::theme_bw(base_size = 26) +
  ggplot2::theme(legend.position = "bottom") + 
  ggplot2::scale_y_continuous(limits = c(2.7, 3.7, 1)) + 
  ggplot2::scale_color_manual(values = met.brewer("Egypt", 3, type = "discrete")) + 
  ggplot2::scale_shape_manual(values = c(15, 15, 15))
